<template>
  <div>
    <div>
      <Form
        ref="formValidate"
        :model="formValidate"
        :rules="ruleValidate"
        :label-width="80"
      >
        <Row justify="space-between">
          <Col>
            <FormItem label="出库单号" prop="did">
              <Input
                v-model="formValidate.did"
                placeholder="输入出库单编号"
                disabled
              ></Input>
            </FormItem>
          </Col>
          <Col>
            <FormItem label="出库人" prop="staff">
              <Input v-model="formValidate.staff" disabled />
            </FormItem>
          </Col>

          <!-- <Col span="6">
            <FormItem label="仓库号" prop="sid">
              <Select v-model="formValidate.sid" clearable filterable placeholder="选择仓库号">
                <Option 
                v-for="item in sids"
                :value="item.value"
                :key="item.value"
                >{{item.value}}</Option>
              </Select>
            </FormItem>
          </Col> -->
          <Col>
            <FormItem label="作物名" prop="cid">
              <Select
                @on-change="tomax"
                v-model="formValidate.cid"
                clearable
                filterable
                placeholder="选择作物名"
              >
                <Option
                  v-for="item in cids"
                  :value="item.value"
                  :key="item.value"
                  >{{ item.value }}</Option
                >
              </Select>
            </FormItem>
          </Col>
          <Col>
            <FormItem label="数量" prop="num">
              <InputNumber
                :min="1"
                :max="formValidate.maxnum"
                v-model="formValidate.num"
              ></InputNumber>
            </FormItem>
          </Col>
          <Col span="6">
            <FormItem label="最大值" prop="maxnum">
              <Input type="text" v-model="formValidate.maxnum" disabled />
            </FormItem>
          </Col>
        </Row>
        <Row justify="space-between">
          <Col span="5">
            <FormItem label="订单号" prop="oid">
              <Select
                @on-change="tooid"
                v-model="formValidate.oid"
                clearable
                filterable
                placeholder="选择订单号"
              >
                <Option
                  v-for="item in oids"
                  :value="item.value"
                  :key="item.value"
                  >{{ item.value }}</Option
                >
              </Select>
            </FormItem>
          </Col>

          <Col span="19">
            <FormItem label="备注" prop="desc">
              <Input
                v-model="formValidate.desc"
                type="textarea"
                :autosize="{ minRows: 2, maxRows: 5 }"
                placeholder="Enter something..."
              ></Input>
            </FormItem>
          </Col>
        </Row>
        <FormItem>
          <Button type="primary" @click="handleSubmit('formValidate')"
            >提交</Button
          >
          <Button @click="handleReset('formValidate')" style="margin-left: 8px"
            >重置</Button
          >
        </FormItem>
      </Form>
    </div>
    <div>
      <yl :datas="datas" @removeone="removeone" />
      <Form>
        <FormItem>
          <Button type="primary" @click="confirm">确认</Button>
          <Button @click="clear" style="margin-left: 8px">清空</Button>
        </FormItem>
      </Form>
    </div>
    <bill
      :datas="billdata"
      billname="出库单"
      :shows="sw"
      :didn="formValidate.did"
      @c1="cc"
      @c2="c2"
    />
  </div>
</template>
<script>
import bill from "./bill.vue";
import yl from "../chart/table.vue";
export default {
  data() {
    return {
      // staff:[],
      sw: false,
      oid_info: [],
      billdata: [],
      cids: [],
      datas: [],
      rdata: [],
      oids: [],
      formValidate: {
        did: "0000000001",
        staff: JSON.parse(sessionStorage.getItem("user")).name,
        // sid: "",
        cid: "",
        // batch: "",
        oid: "",
        num: 0,
        maxnum: 100,
        date: "",
        desc: "",
      },
      ruleValidate: {
        did: [
          {
            required: true,
            message: "出库单号不能为空",
            trigger: "blur",
          },
        ],
        staff: [
          {
            required: true,
            message: "选择出库员姓名",
            trigger: "change",
          },
        ],

        // sid: [
        //   {
        //     required: true,
        //     type: "string",
        //     message: "请选择仓库",
        //     trigger: "change",
        //   },
        // ],
        cid: [
          {
            required: true,
            type: "string",
            message: "请选择作物",
            trigger: "change",
          },
        ],
        // batch: [
        //   {
        //     required: true,
        //     type: "string",
        //     message: "请选择批次",
        //     trigger: "change",
        //   },
        // ],
        num: [
          {
            required: true,
            message: "请输入正确的数量",
            trigger: "blur",
            type: "number",
          },
        ],
      },
    };
  },
  mounted() {
    this.axios
      .get("/api/crop")
      .then((r) => {
        this.rdata = r.data;
        this.cids = [];
        for (var it in this.rdata) {
          this.cids.push({
            label: this.rdata[it].zid,
            value: this.rdata[it].zname,
          });
        }
      })
      .catch((e) => {
        this.$Message.error("E" + e);
      });
    this.axios
      .post("/api/create", { ctype: "cdid", type: "入库", city: "" })
      .then((r) => {
        this.formValidate.did = r.data;
      })
      .catch((e) => {
        this.$Message.error("e" + e);
      });

    var param = JSON.stringify({ action: "list" });
    this.axios
      .get("/api/ss", {
        params: {
          param: param,
        },
      })
      .then((r) => {
        this.oids = r.data;
      })
      .catch((E) => {
        this.$Message.error("e" + E);
      });
  },
  components: {
    yl,
    bill,
  },
  methods: {
    get_date() {
      var today = new Date();
      var y = today.getFullYear();
      var m = today.getMonth() + 1;
      var d = today.getDate();
      m = m < 10 ? "0" + m : m;
      d = d < 10 ? "0" + d : d;
      this.formValidate.date = y + "-" + m + "-" + d;
    },
    handleSubmit(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          // var today = new Date();
          // var y = today.getFullYear();
          // var m = today.getMonth() + 1;
          // var d = today.getDate();
          // m = m < 10 ? "0" + m : m;
          // d = d < 10 ? "0" + d : d;
          // this.formValidate.date = y + "-" + m + "-" + d;
          this.get_date()
          var one = JSON.parse(JSON.stringify(this.formValidate));
          this.datas.push(one);
          this.$Message.success("Success!");
        } else {
          this.$Message.error("Fail!");
        }
      });
    },
    cc(data) {
      this.sw = data;
    },
    c2(value){
      this.sw=value
    },

    handleReset(name) {
      this.$refs[name].resetFields();
    },

    confirm() {
      // this.$Message.info("提交成功");
      // this.datas = [];
      var crops = [];
      var infos = [];
      for (var it in this.datas) {
        var one = { name: this.datas[it].cid, num: this.datas[it].num };
        var info = {
          dates: this.formValidate.date,
          staff: this.formValidate.staff,
          did: this.formValidate.did,
          remark: this.formValidate.desc,
          cid: "",
          type: "出库",
        };
        for (var i in this.rdata) {
          if (this.rdata[i].value == one.name) {
            info.cid = this.rdata.label;
            break;
          }
        }
        infos.push(info);
        crops.push(one);
      }

      var to_tornado = { state: "出库", crops: crops, info: infos };
      this.axios
        .put("/api/storge", to_tornado)
        .then((r) => {
          this.datas = [];
          this.billdata = r.data;
          this.sw = true;
          var td={action:"state",oid:this.formValidate.oid}
          this.put_state(td)
        })
        .catch((e) => {
          this.$Message.error("e" + e);
        });
    },
    put_state(to_tornado){
        this.axios.put("/api/ss",to_tornado).then((r)=>{
          this.$Message.info(r.data)
        }).catch((e)=>{
          this.$Message.error("E"+e)
        })
    },

    clear() {
      this.datas = [];
    },

    removeone(index) {
      console.log(index);
      this.datas.splice(index, 1);
    },

    tomax(value) {
      var flag = false;
      for (var it in this.rdata) {
        if (value == this.rdata[it].zname) {
          this.formValidate.maxnum = this.rdata[it].cropsum;
          flag = true;
          break;
        }
      }
      if (!flag) {
        this.formValidate.maxnum = 100;
      }
    },
    tooid() {
      var param = {
        action: "info",
        oid: this.formValidate.oid,
      };
      this.axios
        .get("/api/ss", { params: { param: JSON.stringify(param) } })
        .then((r) => {
          this.oid_info = r.data;
          var list_info = [];
          this.get_date()
          for (var i in this.oid_info) {
            var one = {
              did: this.formValidate.did,
              staff: this.formValidate.staff,
              cid: this.oid_info[i].name,
              num: this.oid_info[i].num,
              date:this.formValidate.date,
              desc:"无"
            };
            list_info.push(one)
          }
          this.datas=list_info
        })
        .catch((e) => {
          this.$Message.error("e" + e);
        });
    },
  },
};
</script>
